<!doctype html>
<!--
 * Copyright 2014 The Chromium Authors. All rights reserved.  Use of this
 * source code is governed by a BSD-style license that can be found in the
 * LICENSE file.
-->
<html>
  <head>
    <script type="text/javascript">
      var targetExtensionId = 'ojlpikhmmlahaplmjphhfbbijkpgildg';

      var onPostMessageReceived = function(e) {
        var receiverIsComponent = e.data == 'to_component';
        var embedderWindowChannel = e.source;

        var finish = function(
            success, opt_guestProcessId, opt_guestRenderFrameRoutingId) {
          var data = {result: success ? 'SUCCESS' : 'FAILURE'};
          if (success) {
            data.guestProcessId = opt_guestProcessId;
            data.guestRenderFrameRoutingId = opt_guestRenderFrameRoutingId;
          }
          embedderWindowChannel.postMessage(JSON.stringify(data), '*');
        }

        // We're in a guest view, so need to get the chrome.runtime of the
        // parent window.
        var runtime = window.parent.chrome.runtime;
        if (!runtime) {
          finish(false);
          return;
        }

        // The message we send to the |targetExtensionId| ('abc') is arbitrary
        // and unused by the receiver extension.
        runtime.sendMessage(targetExtensionId, 'abc', null,
            function(response) {
          if (response.result === 'defined' && receiverIsComponent) {
            // MessageSender.guestProcessId should be defined if the receiver
            // extension is a component extension.
            finish(true, response.guestProcessId,
                   response.guestRenderFrameRoutingId);
          } else if (response.result === 'undefined' && !receiverIsComponent) {
            // MessageSender.guestProcessId should not be defined if the
            // receiver extension is not a component extension.
            finish(true);
          } else {
            finish(false);
          }
        });
      };
      addEventListener('message', onPostMessageReceived, false);
    </script>
  </head>
  <body>
    <div>This is a guest that sends a message to a component extension.</div>
    <script>
      window.console.log('Guest loaded');
    </script>
  </body>
</html>
